// pages/house/houseUndercarriage/houseUndercarriage.js
import api from "../../../common/api.js";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    reason: '', //下架理由
    delId: '', // 房源ID
    state: '', // 房源状态
    style: '', //下架房源类型
    houseId: '', //唯一房源id
    invalidType: 1, //失效方式
    config: {
      RENT: [{
          name: '3',
          value: '由我出租'
        },
        {
          name: '4',
          value: '由他人出租'
        },
        {
          name: '5',
          value: '业主不租了'
        },
        {
          name: '6',
          value: '其他原因停止交易'
        }
      ],
      SALES: [{
          name: '0',
          value: '由我出售'
        },
        {
          name: '1',
          value: '由他人出售'
        },
        {
          name: '2',
          value: '业主不卖了'
        },
        {
          name: '6',
          value: '其他原因停止交易'
        }
      ],
      both: [{
          name: '3',
          value: '由我出租'
        },
        {
          name: '4',
          value: '由他人出租'
        },
        {
          name: '5',
          value: '业主不租了'
        },
        {
          name: '0',
          value: '由我出售'
        },
        {
          name: '1',
          value: '由他人出售'
        },
        {
          name: '2',
          value: '业主不卖了'
        },
        {
          name: '6',
          value: '其他原因停止交易'
        }
      ],
      new: [{
          name: '7',
          value: '楼盘售罄'
        },
        {
          name: '8',
          value: '停止合作'
        },
        {
          name: '6',
          value: '其他原因停止交易'
        },
      ]
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type === 'new') {
      this.setData({
        style: 'new',
        delId: options.delId,
      })
    } else {
      this.setData({
        style: options.style,
        delId: options.delId,
        state: options.state,
        houseId: options.houseId,
        invalidType: options.invalidType,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  radioChange: function (e) {
    let that = this;
    that.setData({
      reason: e.detail.value
    })
  },

  onSave() {
    if (this.data.style === 'new') {
      this.downNewHouseById()
    } else {
      this.save()
    }
  },

  save() {
    const {
      delId,
      state,
      reason,
      houseId,
      invalidType
    } = this.data
    if (!reason) {
      wx.showToast({
        title: '请选择下架理由',
        icon: 'none'
      })
      return
    }
    let id = {
      delId: delId,
      state: state === "AVAILABLE" ? "UNAVAILABLE" : "AVAILABLE",
      reason: reason,
      houseId: houseId,
      invalidType: invalidType,
    }
    api.houseFail('', id, (res) => {
      wx.reLaunch({
        url: '/pages/index/index?isUpdate=true'
      })
    })
  },

  downNewHouseById() {
    return new Promise((resolve, reject) => {
      const {
        delId,
        reason,
      } = this.data
      if (!reason) {
        wx.showToast({
          title: '请选择下架理由',
          icon: 'none'
        })
        return
      }
      api.downNewHouseById({
        reasonCode: reason,
        delId,
        state: 'UNAVAILABLE',
        invalidType: '1'
      }, (res) => {
        wx.reLaunch({
          url: '/pages/index/index?isUpdate=true'
        })
      })
    })
  }


})